﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace TowerDefense
{
    public class Creep : Entity
    {

        private float _direction = 1;

        public double Health
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public double Speed
        {
            get;
            set;
        }

        public System.Collections.Generic.List<TowerDefense.Debuffs.Effect> UnderEffects
        {
            get;
            set;
        }


        public bool IsAir
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public double Armor
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public Queue<Vector2> WayPoints
        { get; set; }

        public Vector2 CurrentWayPoint
        {
            get;
            set;
        }

        public float Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        public float TargetDirection
        {
            get;
            set;
        }

        public Creep.CreepState CurrentState
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }


        public Creep(List<Vector2> wayPoints, bool isTopView)
        {
            this.TopView = isTopView;
            this.WayPoints = new Queue<Vector2>();
            init(wayPoints);
            this.CurrentWayPoint = this.WayPoints.Dequeue();
        }

        private void init(List<Vector2> wayPoints)
        {
            foreach (Vector2 v in wayPoints)
            {
                this.WayPoints.Enqueue(v);
            }
        }

        public bool TopView
        {
            get;
            set;
        }
        

        public override void Update(GameTime gameTime)
        {
            Vector2 delta = CurrentWayPoint - Position;

            if (delta.X < 10 && delta.X > -10 && delta.Y < 10 && delta.Y > -10)
            {
                if (WayPoints.Count < 1)
                {
                    init(World.CurrentMap.Route.Waypoints);
                }

                this.CurrentWayPoint = this.WayPoints.Dequeue();
            }

            
            this.TargetDirection = Library.FindAngle(Position, CurrentWayPoint);

            this.Direction = this.TargetDirection;
            
            //Console.WriteLine("direction: {0} | targetdirection: {1}", this.Direction.ToString(), this.TargetDirection.ToString());

            this.Position = this.Position + Library.MoveDirection(this.Position, this.Speed * gameTime.ElapsedGameTime.Milliseconds, Direction);   
        }

        public enum CreepState
        {
            Moving,
            Dead
        }

        public override Entity Clone()
        {
            throw new NotImplementedException();
        }

        public override void Draw(SpriteBatch sb)
        {
            if(Texture != null)
                sb.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, 20, 20), null, Color.White, Direction > Math.PI / 2 && !this.TopView ? (float)(Direction - Math.PI) : Direction, new Vector2(10, 10), SpriteEffects.None, 0.1f);
        }
    }
}
